﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace _0812045
{
    public class Character : VisibleGameEntity
    {
        public override VisibleGameEntity Clone()
        {

            VisibleGameEntity newObject = new Character();
            //Copy các thuộc tính từ this => newObject 
            newObject._nsprite = this._nsprite;
            newObject._sprite = new CharacterSprite[this._nsprite];
            for (int i = 0; i < this._nsprite; i++)
            {
                newObject._sprite[i] = new CharacterSprite(this._sprite[i].texture2d);
            }
            newObject.x = this.x;
            newObject.y = this.y;
            ((Character)newObject).nDelay = this.nDelay;
            ((Character)newObject).iDelay = 0;

            return newObject;
        }

        public override bool Init(ContentManager content, int n, string strResource)
        {
            // Khoi tao nhan vat
            _nsprite = 1;
            _sprite = new CharacterSprite[_nsprite];
            Texture2D[] texture2D;
            texture2D = new Texture2D[n];
            for (int i = 0; i < n; i++)
                texture2D[i] = content.Load<Texture2D>(@"Characters/" + strResource + i.ToString("00"));
            _sprite[0] = new CharacterSprite(texture2D);

            return true;
        }



        public int nDelay; // Mức độ cập nhật của nhân vật
        protected int iDelay = 0;
        public override void Update(GameTime gameTime)
        {
            for (int i = 0; i < bullets.Count; i++)
            {
                if (((BulletSprite)bullets[i]._sprite[0]).visible == false)
                    bullets.RemoveAt(i);
            }
            for (int i = 0; i < bullets.Count; i++)
            {
                bullets[i].Update(gameTime);
            }
            if (iDelay == 0)
            {
                int i;
                for (i = 0; i < _nsprite; i++)
                    _sprite[i].Update(gameTime);
            }
            iDelay = (iDelay + 1) % nDelay;
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            for (int i = 0; i < _nsprite; i++)
                _sprite[i].Draw(gameTime, spriteBatch);
            for (int i = 0; i < bullets.Count; i++)
            {
                bullets[i].Draw(gameTime,spriteBatch);
            }
        }

        List<Bullet> bullets = new List<Bullet>();

        public void Fire(BulletManager bulletManager)
        {
            
            Bullet bullet = (Bullet)bulletManager.CreateObject(0);
            //bullet.nDelay = 3;
            bullet.x = this.x;
            bullet.y = this.y + 50;
            bullets.Add(bullet);
        }
    }
}
